const AdminBiz = require('../../../../store/admin_biz_config.js');
const PublicBiz = require('../../../../../../config/public_biz_config.js');
// 导入路径辅助模块，避免重复引用
const pathImport = require('../helpers/path_import.js');
const { pageHelper, cloudHelper, validate } = pathImport;
const { createStoreBindings } = require('mobx-miniprogram-bindings');
import { adminStore } from '../../../../config/store_config.js';

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		formName: '',
		formDesc: '',
		formPhone: '',
		formPassword: '',
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		if (!AdminBiz.isAdmin(this, true)) return;
		
		// 创建MobX绑定
		this.storeBindings = createStoreBindings(this, {
			store: adminStore,
			actions: ['insertMgr']
		});
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {
		// 销毁MobX绑定
		this.storeBindings && this.storeBindings.destroyStoreBindings();
	},



	/** 
	 * 数据提交
	 */
	bindFormSubmit: async function () {
		if (!AdminBiz.isAdmin(this, true)) return;

		let data = this.data;

		// 数据校验 
		data = validate.check(data, AdminBiz.CHECK_FORM_MGR_ADD, this);
		if (!data) return;

		try {
			let adminId = this.data.id;
			data.id = adminId;

			// 使用MobX Store中的action
			await this.insertMgr(data);
			
			// 成功后的回调
			let callback = async function () {
				wx.navigateBack();
			}
			pageHelper.showSuccToast('添加成功', 1500, callback);

		} catch (err) {
			console.log(err);
		}

	},
})